package com.lhh.springcloud.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author 李洪豪
 * @Sno 20220301044
 */
//@Component
//public class SelfGatewayFilter implements GlobalFilter, Ordered {
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        System.out.println("gateway的过滤器被执行....");
//        MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();//得到请求的所有参数
//        //例如：http//localhost:20000/member/query?username=lhh&password=123456&username=zyn
//        String username = queryParams.getFirst("username");//得到第一个username的值
//        String password = queryParams.getFirst("password");
//        if (!("lhh".equals(username) && "123456".equals(password))){
//            exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); //  BAD_REQUEST(400, "Bad Request"),
//            //如果不满足条件，不再向下执行
//            return exchange.getResponse().setComplete();
//        }
//        //如果满足条件，继续向下执行
//        return chain.filter(exchange);
//    }
//
//
//    //表示执行过滤器的优先级，返回值越小，优先级越高
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//}
